<?php 
class usersDAL 
{
	public function Insert($db, $usersObj, &$errorMsg) 
	{
		$errorMsg = "";

		$query = "INSERT INTO users (username, password, userroleID, deleteFlag, datetimeLogin, firstname, lastname, email, phone, image, city, country) VALUES (".ToSQLPostVAL($usersObj->username).", ".ToSQLPostVAL($usersObj->password).", ".ToSQLPostVAL($usersObj->userroleID).", ".ToSQLPostVAL($usersObj->deleteFlag).", ".ToSQLPostVAL($usersObj->datetimeLogin).", ".ToSQLPostVAL($usersObj->firstname).", ".ToSQLPostVAL($usersObj->lastname).", ".ToSQLPostVAL($usersObj->email).", ".ToSQLPostVAL($usersObj->phone).", ".ToSQLPostVAL($usersObj->image).", ".ToSQLPostVAL($usersObj->city).", ".ToSQLPostVAL($usersObj->country).")";
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		$usersObj->id = mysql_insert_id();
		return true;
	}

	public function Update($db, $usersObj, &$errorMsg)
	 {	
		$errorMsg = "";

		$query = "UPDATE users SET username=".ToSQLPostVAL($usersObj->username).", password=".ToSQLPostVAL($usersObj->password).", userroleID=".ToSQLPostVAL($usersObj->userroleID).", deleteFlag=".ToSQLPostVAL($usersObj->deleteFlag).", datetimeLogin=".ToSQLPostVAL($usersObj->datetimeLogin).", firstname=".ToSQLPostVAL($usersObj->firstname).", lastname=".ToSQLPostVAL($usersObj->lastname).", email=".ToSQLPostVAL($usersObj->email).", phone=".ToSQLPostVAL($usersObj->phone).", image=".ToSQLPostVAL($usersObj->image).", city=".ToSQLPostVAL($usersObj->city).", country=".ToSQLPostVAL($usersObj->country)." WHERE id=".ToSQLPostVAL($usersObj->id);
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		return true;
	}

	public function Load($db, $id, &$errorMsg, $joinTables="", $onFields="", $getFields="")
	{
		$errorMsg = "";
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$query	= "SELECT users.*$extraFields FROM users $joinClause WHERE users.id=".ToSQLPostVAL($id);

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
			return null;
		} else {
			if ($db->next_record()) {
				$usersObj = new usersObj($db->f("id"), $db->f("username"), $db->f("password"), $db->f("userroleID"), $db->f("deleteFlag"), $db->f("datetimeLogin"), $db->f("firstname"), $db->f("lastname"), $db->f("email"), $db->f("phone"), $db->f("image"), $db->f("city"), $db->f("country"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$usersObj->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				return $usersObj;
			} else {
				$errorMsg = "Data not found.";
				return null;
			}
		}
		return null;
	}

	public function IsExist($db, $whereStr) 
	{
		$query	= "SELECT * FROM users";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else return false;

		if (!$db->query($query)) {
			return false;
		} else {
			while ($db->next_record()) {
				return true;
			}
		}
		return false;
	}

	public function Pure($db, $whereStr, &$errorMsg) 
	{
		$query	= "DELETE FROM users";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else {
			$errorMsg = "You have no delete condition in query string ($query WHERE ?).";
			return false;
		}

		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}
		return true;
	}

	public function GetList($db, $whereStr, $orderByStr, $limit, &$errorMsg, $joinTables="", $onFields="", $getFields="") 
	{
		$returnData = array();
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$errorMsg = "";
		$query	= "SELECT users.*$extraFields FROM users$joinClause";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		if ($orderByStr!="") $query = $query." ORDER BY ".$orderByStr;
		if ($limit!="") $query = $query." LIMIT ".$limit;

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
		} else {
			while ($db->next_record()) 	{
				$objVal = new usersObj($db->f("id"), $db->f("username"), $db->f("password"), $db->f("userroleID"), $db->f("deleteFlag"), $db->f("datetimeLogin"), $db->f("firstname"), $db->f("lastname"), $db->f("email"), $db->f("phone"), $db->f("image"), $db->f("city"), $db->f("country"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$objVal->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				$returnData[] = $objVal;
			}
		}
		return $returnData;
	}

	public function Push($usersObj)
	{
		global $id, $username, $password, $userroleID, $deleteFlag, $datetimeLogin, $firstname, $lastname, $email, $phone, $image, $city, $country;

		$id = $usersObj->id;
		$username = $usersObj->username;
		$password = $usersObj->password;
		$userroleID = $usersObj->userroleID;
		$deleteFlag = $usersObj->deleteFlag;
		$datetimeLogin = $usersObj->datetimeLogin;
		$firstname = $usersObj->firstname;
		$lastname = $usersObj->lastname;
		$email = $usersObj->email;
		$phone = $usersObj->phone;
		$image = $usersObj->image;
		$city = $usersObj->city;
		$country = $usersObj->country;
	}

	public function Pop(&$usersObj)
	{
		global $id, $username, $password, $userroleID, $deleteFlag, $datetimeLogin, $firstname, $lastname, $email, $phone, $image, $city, $country;

		if (isset($id)) $usersObj->id = $id;
		if (isset($username)) $usersObj->username = $username;
		if (isset($password)) $usersObj->password = $password;
		if (isset($userroleID)) $usersObj->userroleID = $userroleID;
		if (isset($deleteFlag)) $usersObj->deleteFlag = $deleteFlag;
		if (isset($datetimeLogin)) $usersObj->datetimeLogin = $datetimeLogin;
		if (isset($firstname)) $usersObj->firstname = $firstname;
		if (isset($lastname)) $usersObj->lastname = $lastname;
		if (isset($email)) $usersObj->email = $email;
		if (isset($phone)) $usersObj->phone = $phone;
		if (isset($image)) $usersObj->image = $image;
		if (isset($city)) $usersObj->city = $city;
		if (isset($country)) $usersObj->country = $country;
	}

} 
?>